//
//  UIImageView+Extensions.swift
//  Extensions
//
//  Created by gupengling on 2021/6/5.
//  Copyright © 2021 gupengling. All rights reserved.
//

import UIKit
//import SDWebImage
public extension WDExtension where Base: UIImageView {
    @discardableResult
    func image(_ a:UIImage?) -> WDExtension {
        base.image = a
        return self
    }
    @discardableResult
    func highlighted(_ image:UIImage?) -> WDExtension {
        base.highlightedImage = image
        return self
    }
    @discardableResult
    func isHighlighted(_ a:Bool) -> WDExtension {
        base.isHighlighted = a
        return self
    }
    @discardableResult
    func animation(_ images:[UIImage]?) -> WDExtension {
        base.animationImages = images
        return self
    }
    
    @discardableResult
    func highlighted(_ animationImages:[UIImage]?) -> WDExtension {
        base.highlightedAnimationImages = animationImages
        return self
    }
    
    @discardableResult
    func animation(_ duration:TimeInterval) -> WDExtension {
        base.animationDuration = duration
        return self
    }
    @discardableResult
    func animation(_ repeatCount:Int) -> WDExtension {
        base.animationRepeatCount = repeatCount
        return self
    }
    @discardableResult
    func startAnimating() -> WDExtension {
        base.startAnimating()
        return self
    }
    @discardableResult
    func stopAnimating() -> WDExtension {
        base.stopAnimating()
        return self
    }
    
    @discardableResult
    func animation(_ images:[UIImage], duration:TimeInterval = 1, repeatCount:Int = 1) -> WDExtension {
        base.animationImages = images
        base.animationDuration = duration
        base.animationRepeatCount = repeatCount
        base.startAnimating()
        return self
    }
    @discardableResult
    func animation(_ names:[String], duration:TimeInterval = 1, repeatCount:Int = 1) -> WDExtension {
        //let images:[UIImage] = try names.map{ UIImage(named: $0)}
        var images:[UIImage] = []
        for item in names {
            guard let img = UIImage(named: item) else{
                assertionFailure("👉👉👉 UIImage(named: \(item))错误，请检查 names")
                continue
            }
            images.append(img)
        }
        guard images.count > 0 else {
            return self
        }
        base.animationImages = images
        base.animationDuration = duration
        base.animationRepeatCount = repeatCount
        base.startAnimating()
        return self
    }
}

public extension UIImageView {
    
    /// 图片名字
    /// 快速通过图片名字生成图片
    /// - Parameter imageName: 图片
    convenience init(imageName: String) {
        self.init()
        let image = UIImage(named: imageName)
        self.image = image
    }
//    
//    /// 封装SDWebImageView图片中间层,有占位图片
//    ///
//    /// - Parameters:
//    ///   - urlStr: 图片url字符串
//    ///   - placeHoder: 占位图的名字
//    func cl_setImage(urlStr: String, placeHoder: String) {
//        
//        let url = URL(string: urlStr)
//        let pImage = UIImage(named: placeHoder)
//        if let url = url, let pImage = pImage {
//            self.sd_setImage(with: url, placeholderImage: pImage)
//        }
//    }
//    
//    /// 封装SDWebImageView图片中间层，不需要占位图片
//    ///
//    /// - Parameters:
//    ///   - urlStr: 图片url字符串
//    ///   - placeHoder: 占位图的名字
//    func cl_setImage(urlStr: String) {
//        let url = URL(string: urlStr)
//        if let url = url {
//            self.sd_setImage(with: url)
//        }
//    }

    /// 根据颜色创建图片
    class  func colorImage(_ color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? nil
    }
}
